package net.kazed.nextaction.tools;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import net.kazed.nextaction.R;
import net.kazed.nextaction.database.GtdProvider;

public class ToolsActivity extends ListActivity {
   
   private static final int PURGE = 0;
   private static final int RESET_DATABASE = 1;
	
    @Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		
        setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT);
	}
    
    @Override
    protected void onResume() {
       super.onResume();
       Resources resources = getResources();
       String[] tools = new String[] {
             resources.getString(R.string.tools_purge),
             resources.getString(R.string.tools_reset_database)
       };
         
         ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, R.layout.list_item_view,
               R.id.name, tools);
         setListAdapter(adapter);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
      switch (position) {
      case PURGE:
         startActivity(new Intent(this, PurgeActivity.class));
         break;
      case RESET_DATABASE:
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setTitle(R.string.tools_reset_database__alert_title).setMessage(R.string.tools_reset_database__alert_message)
            .setIcon(android.R.drawable.ic_dialog_alert);
         builder.setPositiveButton(R.string.alert_yes, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int whichButton) {
                  GtdProvider.DatabaseHelper helper = new GtdProvider.DatabaseHelper(ToolsActivity.this);
                  helper.onUpgrade(helper.getWritableDatabase(), 0, 10);
               }
         });
         builder.setNegativeButton(R.string.alert_no, null);
         builder.show();
         break;
      }       
    }

}
